<?php

namespace App\Http\Controllers\V1\Admin;

use App\Models\V1\Menus;
use App\Models\V1\Submenus;
use App\Models\V1\AdminUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Log;
class DashboardController extends Controller
{
    /**
     * 显示后台界面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $currentUser = Auth::user();
        // 获取登录用户基本信息
        $data['logindata'] = $this->getUserInfo($currentUser);
//        DD($data['logindata']);
        // 获取登录用户显示主菜单信息
        $data['menudata'] = $this->getUserMenu();

        // 默认子菜单
        $data['submenudata'] = $this->getUserSubmenu('m100');
//        Log::info('data  submenudata  submenudata',$data);


        return view('v1.dashboard', $data);
    }

    /**
     * 显示后台当前子菜单界面
     *
     * @param $code
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function submenus($code)
    {
        // 获取登录用户显示子菜单信息
        $data['submenudata'] = $this->getUserSubmenu($code);
        return view('v1.submenu', $data);
    }

    /**
     * 解除锁屏操作
     *
     * @param Request $request
     */
    public function unlockScreen(Request $request)
    {
    }

    ///////////////////////////////////////////////////////////////////
    /// 获取数据功能函数
    ///////////////////////////////////////////////////////////////////
    /**
     * 获取登录用户基本信息
     * @return array
     */
    private function getUserInfo(AdminUser $user)
    {
        $flag = $user['is_staff'];

        // 登录用户信息返回格式
        $loginData = array(
            'is_staff' => $flag,
            'code' => '',
            'name' => '',
            'avatar' => '',
        );

        if ($flag) {
            $staff = $user->faculty;
            $loginData['code'] = $staff['code'];
            $loginData['name'] = $staff['name'];
            $loginData['avatar'] = empty($staff['avatar']) ? env('APP_URL') . '/images/default.jpg' : $staff['avatar'];
        } else {
            $students = $user->student;
            $loginData['code'] = $students['code'];
            $loginData['name'] = $students['name'];
            $loginData['avatar'] = empty($students['avatar']) ? env('APP_URL') . '/images/default.jpg' : $students['avatar'];
        }
        return $loginData;
    }

    /**
     * 获取登录用户主菜单信息
     * @return array
     */
    private function getUserMenu()
    {
        // 返回值数组
        $menuList = array();
        // 一级临时数组
        $first = array();
        // 二级临时数组
        $second = array();

        // 数据库加载主菜单数据
        $menus = Menus::orderBy('show_level', 'asc')->get(['*']);

        // 区分一二级菜单
        foreach ($menus as $menu) {
            if ($menu->parent_code === '0') {
                array_push($first, $menu->toArray());
            } else {
                array_push($second, $menu->toArray());
            }
        }

        // 整合主菜单
        foreach ($first as $fm) {
            $fm['sub'] = array();
            $tmp = array();

            foreach ($second as $sm) {
                if ($sm['parent_code'] === $fm['code']) {
                    array_push($tmp, $sm);
                }
            }
            $fm['sub'] = $tmp;
            array_push($menuList, $fm);
        }

        return $menuList;
    }

    /**
     * 获取登录用户当前主菜单对应子菜单信息
     * @param $code
     * @return array
     */
    private function getUserSubmenu($menuCode)
    {
        // 返回值数组
        $submenuList = array();
        // 一级临时数组
        $first = array();
        // 二级临时数组
        $second = array();

        // 数据库加载主菜单数据
        $submenus = Submenus::where('menu_code', $menuCode)->orderBy('show_level', 'asc')->get(['*']);

        // 区分一二级菜单
        foreach ($submenus as $sub) {
            if ($sub->parent_code === '0') {
                if(($sub->name !== '学校和校区')&&($sub->name !== '校内部门')&&($sub->name !== '优秀学子')){
                    if($sub->name == '公告发布'){
                        $sub->name = '致新生';
                    }
                    array_push($first, $sub->toArray());
                }
            } else {
                array_push($second, $sub->toArray());
            }
        }

        // 整合主菜单
        foreach ($first as $fm) {
            $fm['sub'] = array();
            $tmp = array();

            foreach ($second as $sm) {
                if ($sm['parent_code'] === $fm['code']) {
                    array_push($tmp, $sm);
                }
            }
            $fm['sub'] = $tmp;
            array_push($submenuList, $fm);
        }
        return $submenuList;
    }
}
